package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.QrCodeDebitFeeRateData;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 2017/9/12 15:12.
 */

public class ChooseDebitChannelAdapter extends CommonAdapter<QrCodeDebitFeeRateData.FeeRate> {
    private int selected=0;
    private double inputAmount;

    public ChooseDebitChannelAdapter(Context mContext, List<QrCodeDebitFeeRateData.FeeRate> mData,double inputAmount) {
        super(mContext, mData, R.layout.item_choose_channel);
        this.inputAmount=inputAmount;
    }

    @Override
    public void convert(ViewHolder holder, QrCodeDebitFeeRateData.FeeRate item, int position) {
        TextView tvChannel=holder.getView(R.id.tv_channel);
        switch (item.getChannel_type()){
            case Constant.KQB010102:
                tvChannel.setText("微信");
                break;
            case Constant.KQB010103:
                tvChannel.setText("支付宝");
                break;
            case Constant.KQB010104:
                tvChannel.setText("京东");
                break;
            case Constant.KQB010105:
                tvChannel.setText("QQ");
                break;
        }
        ImageLoaderUtil.displayImage(
                URLUtil.CBB_IMG_URL+item.getPicture_path(),
                (ImageView) holder.getView(R.id.iv_channel));
        if(canSelect(position)){
            holder.getConvertView().setAlpha(1.0f);
        }else{
            holder.getConvertView().setAlpha(0.5f);
        }
        if(position==selected){
            holder.getView(R.id.iv_selected).setVisibility(View.VISIBLE);
        }else{
            holder.getView(R.id.iv_selected).setVisibility(View.INVISIBLE);
        }
    }

    private double getChannelQuota(QrCodeDebitFeeRateData.FeeRate rate){
        double limitQuota;
        try{
            limitQuota=Double.parseDouble(rate.getHigh_position());
        }catch (Exception e){
            e.printStackTrace();
            limitQuota=0.00D;
        }
        return limitQuota;
    }

    public boolean canSelect(int position){
        return getChannelQuota(mData.get(position))>=inputAmount;
    }

    public void setSelected(int position){
        this.selected=position;
        notifyDataSetChanged();
    }

    public int getSelected(){
        return this.selected;
    }

}
